<?php

class Cetak extends CI_Controller{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('m_cetak');
		$this->load->library('pagination');
		$this->session_dt('SET');
	}
	public function session_dt($set=0){
		if(!$set){
			///echo 'iam GET '.'<br/>';
			return array(	'dt_start'=>$this->session->userdata('cetak_dt_start'),
							'dt_end'=>$this->session->userdata('cetak_dt_end') );
		}

		if($dt_start = $this->input->post('dt_start')){
			$this->session->set_userdata('cetak_dt_start' , ID_DateToMySQL($dt_start));
			$this->session->set_userdata('cetak_orig_dt_start' ,($dt_start));//echo 'iam SET'.'<br/>';
		}
		if($dt_end = $this->input->post('dt_end')){
			$this->session->set_userdata('cetak_dt_end' , ID_DateToMySQL($dt_end ));
			$this->session->set_userdata('cetak_orig_dt_end' , ($dt_end));//echo 'iam SET'.'<br/>';
		}
		//echo 'iam USE EXISTENT'.'<br/>';
	}
	public function count()
	{
		$dt = $this->session_dt();
		$dt_start = $dt['dt_start'];
		$dt_end = $dt['dt_end'];
		echo $this->m_cetak->record_count($dt_start,$dt_end);
	}
	public function main($offset=0)
	{
		//if($offset == )
		$doExcel = FALSE;
		if($this->input->post('do_excel') == 'YES' )
		{
			$doExcel = TRUE;
			$filename ="cetak.xls";
		    header('Content-type: application/excel');
			//$filename = 'filename.xls';
			header('Content-Disposition: attachment; filename='.$filename);
		}
		if($this->input->post('do_cetak') == 'YES' || ($offset > 0 || $offset==''))
		{
			$dt = $this->session_dt();
			$dt_start = $dt['dt_start'];
			$dt_end = $dt['dt_end'];
			//print_r($dt);
			$config['base_url'] = site_url('cetak/main');
			$config['total_rows'] = $this->m_cetak->record_count($dt_start,$dt_end);
			$config['per_page'] = 250;
			$config['uri_segment'] = 3;
			$config['num_links'] = 3;
			// $config['full_tag_open'] = '<ul>';
			// $config['full_tag_close'] = '</ul>';
			// $config['first_link'] = '<li>First</li>';
			// $config['first_tag_open'] = '<div>';
			// $config['first_tag_close'] = '</div>';
			// $config['last_link'] = 'Last';
			// $config['last_tag_open'] = '<div>';
			// $config['last_tag_close'] = '</div>';
			// $config['next_link'] = '&gt;';
			// $config['next_tag_open'] = '<div>';
			// $config['next_tag_close'] = '</div>';
			// $config['prev_link'] = '&lt;';
			// $config['prev_tag_open'] = '<div>';
			// $config['prev_tag_close'] = '</div>';
			// $config['cur_tag_open'] = '<b>';
			// $config['cur_tag_close'] = '</b>';
			
			$this->pagination->initialize($config);
				
			$total_row = 500;
			if($doExcel){
				$offset = 0;
				$total_row = $config['total_rows'];
			}
			$data=array(
				'param' => array(
					'dt_start' => $this->session->userdata('cetak_orig_dt_start'),
					'dt_end' => $this->session->userdata('cetak_orig_dt_end')
				),
				'rs' => $this->m_cetak->fetch_cetak($offset,$total_row,$dt_start,$dt_end),
				'pgn' => $this->pagination->create_links(),
				'num' => $offset,
				'do_excel' => $doExcel
			);
			//die($this->db->last_query());

			$this->load->view('_cetak_table',$data);
		}
		else
			$this->load->view('_cetak');
	}
}